package gui;

// Swing specific imports
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

/**
 * The HuidigeStaat class creates the components under the HuidigeStaat tab
 * 
 * @version 31-03-2009
 */
public class HuidigeStaat implements MouseListener {

	// The panel which is going to hold all the components
	private JPanel panel;

	// The seperator between the title and the actual data
	private JSeparator sep_Statistics;

	// The labels for the different titles
	private JLabel label_Statistics;
	private JLabel label_Light;
	private JLabel label_Humidity;
	private JLabel label_Temperature;

	// The variables containing the sensor values
	private JLabel var_Light;
	private JLabel var_Humidity;
	private JLabel var_Temperature;

	// The types, which are the suffixes for the sensor values
	private JLabel type_Light;
	private JLabel type_Humidity;
	private JLabel type_Temperature;

	private String format_temperature;

	/**
	 * Constructor creates the lables
	 */
	public HuidigeStaat() {
		format_temperature = "C";

		// Labels
		label_Statistics = new JLabel("Statistieken");
		label_Light = new JLabel("Licht");
		label_Humidity = new JLabel("Vochtigheid");
		label_Temperature = new JLabel("Temperatuur");

		// Vars
		var_Light = new JLabel("[light]");
		var_Humidity = new JLabel("[hum]");
		var_Temperature = new JLabel("[temp]");

		// Types
		type_Light = new JLabel("%");
		type_Humidity = new JLabel("%");
		type_Temperature = new JLabel("\u00B0C");

		// Seperator
		sep_Statistics = new JSeparator();

		// Make the temperature label clickable
		label_Temperature.addMouseListener(this);
	}

	/**
	 * Sets the values for the variables
	 * 
	 * @param humidity The new value for humidity
	 * @param light The new value for light
	 * @param temperature The new value for temperature
	 */
	public void setText(String humidity, String light, String temperature) {
		var_Humidity.setText(humidity);
		var_Light.setText(light);

		if (this.format_temperature.equals("C")) {
			var_Temperature.setText(temperature);
		} else if (this.format_temperature.equals("F")) {
			var_Temperature.setText(this.convertToFahrenheit(temperature + ""));
		}
	}

	/**
	 * Creates the panel containing all the elements on this tab
	 * 
	 * @return The HuidigeStaat panel
	 */
	public JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new GroupLayout());
			panel.add(label_Statistics, new Constraints(new Leading(6, 12, 12), new Leading(10, 12, 12)));
			panel.add(sep_Statistics, new Constraints(new Leading(6, 151, 12, 12), new Leading(31, 10, 12, 12)));
			panel.add(label_Light, new Constraints(new Leading(6, 12, 12), new Leading(35, 12, 12)));
			panel.add(var_Light, new Constraints(new Leading(113, 12, 12), new Leading(37, 12, 12)));
			panel.add(type_Light, new Constraints(new Leading(135, 12, 12), new Leading(37, 12, 12)));
			panel.add(label_Temperature, new Constraints(new Leading(6, 12, 12), new Leading(58, 12, 12)));
			panel.add(var_Temperature, new Constraints(new Leading(113, 12, 12), new Leading(59, 12, 12)));
			panel.add(type_Temperature, new Constraints(new Leading(135, 12, 12), new Leading(59, 12, 12)));
			panel.add(label_Humidity, new Constraints(new Leading(6, 12, 12), new Leading(80, 12, 12)));
			panel.add(var_Humidity, new Constraints(new Leading(113, 12, 12), new Leading(79, 12, 12)));
			panel.add(type_Humidity, new Constraints(new Leading(135, 12, 12), new Leading(79, 12, 12)));
		}
		return panel;
	}

	/**
	 * Listens for the user clicking on the temperature label
	 * 
	 * @param e The click event
	 */
	public void mouseClicked(MouseEvent e) {
		if (this.format_temperature.equals("C")) {
			this.format_temperature = "F";

			var_Temperature.setText(convertToFahrenheit(this.var_Temperature.getText()));
			this.type_Temperature.setText("\u00B0F");
		} else if (this.format_temperature.equals("F")) {
			this.format_temperature = "C";

			var_Temperature.setText(convertToCelcius(this.var_Temperature.getText()));
			this.type_Temperature.setText("\u00B0C");
		}
	}

	/**
	 * Converts the specified temperature as string to Celcius
	 * 
	 * @param temp_f The current temperature as string in Fahrenheit
	 * @return The temperature in Celcius as string
	 */
	private String convertToCelcius(String temp_f) {
		double temp_c = Integer.valueOf(temp_f);
		temp_c = (temp_c - 32) / 1.8;

		return Math.round(temp_c) + "";
	}

	/**
	 * Converts the specified temperature as string to Fahrenheit
	 * 
	 * @param temp_c The current temperature as string in Celcius
	 * @return The temperature in Fahrenheit as string
	 */
	private String convertToFahrenheit(String temp_c) {
		double temp_f = Integer.valueOf(temp_c);
		temp_f = temp_f * 1.8 + 32;

		return Math.round(temp_f) + "";
	}

	/**
	 * Listen for mouse entering
	 */
	public void mouseEntered(MouseEvent e) {

	}

	/**
	 * Listen for mouse exiting
	 */
	public void mouseExited(MouseEvent e) {

	}

	/**
	 * Listen for mouse pressing
	 */
	public void mousePressed(MouseEvent e) {

	}

	/**
	 * Listen for mouse releasing
	 */
	public void mouseReleased(MouseEvent e) {

	}
}
